<?php

class GameWorker extends Worker{
	
       // private $runData;
	//0方  1梅 2红 3黑
	//10-0 11-1 12-2 13-3 A-4
	protected  $pkList = array(array(0,0),array(0,1),array(0,2),array(0,3),array(0,4),
			array(1,0),array(1,1),array(1,2),array(1,3),array(1,4),
			array(2,0),array(2,1),array(2,2),array(2,3),array(2,4),
			array(3,0),array(3,1),array(3,2),array(3,3),array(3,4));
	/**
	 * 给接口中返回房间信息
	 * @param unknown_type $gameInfo
	 */
	protected function reGameInfo($gameInfo,$roomID){
		$newUserInfo = pkReturnUserInfo($gameInfo['userInfo'],$roomID);
		if(!$newUserInfo){
			$this->socketWrite(array('code'=>0,'data'=>'获取房间信息失败'));return;
		}
		$gameInfo['userInfo'] = $newUserInfo;
		if($gameInfo['status'] < 3){//未发公共牌
			$gameInfo['pk'] = array();
		}
		$this->socketWrite(array('code'=>1,'error'=>'','data'=>$gameInfo));return;
	}
	/**
	 * 将用户信息根据座位排序
	 * @param unknown_type $userInfo
	 * @return unknown
	 */
	protected function sortBySeat($userInfo){
		//$newUserInfo = array();
		if(is_array($userInfo)){
			for($i=0;$i<count($userInfo)-1;$i++){
				for($j=$i+1;$j<count($userInfo);$j++){
					if($userInfo[$i]['seat']>$userInfo[$j]['seat']){//换位
						$c = $userInfo[$i];
						$userInfo[$j] = $userInfo[$i];
						$userInfo[$i] = $c;
					}
				}
			}
		}
		return $userInfo;
	}
	/**
	 * 返回下当前座位号的下一个玩家座位
	 * @param unknown_type $userInfo
	 * @param unknown_type $preBaner
	 * @return unknown|Ambigous <number, unknown>
	 */
	protected function nextSeat($userInfo,$Seat){
		//$userInfo = $this->sortBySeat($userInfo);
		//$maxUserNum = 5;//房间最大人数		
		$maxSeat = -1;
		foreach ($userInfo as $skey=>$user){
			if($skey > $maxSeat){
				$maxSeat = $skey;
			}
		}				
		$Seat = $Seat>=$maxSeat?-1:$Seat;
		foreach ($userInfo as $skey=>$user){
			if($skey > $Seat){
				return $skey;break;
			}
		}
		return $Seat;
	}
	/**
	 * 下一个行动者
	 * @param unknown_type $userInfo
	 */
	protected function nextAct($userInfo,$Seat,$beginSeat){					
		$nextAct = $this->nextSeat($userInfo, $Seat);				
		if($nextAct == $beginSeat)
			return $beginSeat;
		if($userInfo[$nextAct]['status'] == 0){
			$nextAct = $this->nextAct($userInfo, $nextAct,$beginSeat); 
		}
		return $nextAct;
	}
	/**
	 * 返回游戏房间信息
	 * @param unknown_type $roomID
	 * @param unknown_type $userID
	 * @return void|Ambigous <multitype:, mixed>
	 */
	protected function gameInfo($roomID,$userID){
		$gameModel = $this->loadModel("Game");
		//获取房间信息
		$gameInfo  = $gameModel->getGameInfo(array("roomID"=>$roomID));
		if(!$gameInfo){
			$this->socketWrite(array('code'=>0,'data'=>'room not created'));return;
		}
		$userInfo = $gameInfo['userInfo'];
		$inSeat = false;
		if(is_array($userInfo)){
			foreach($userInfo as &$user){
				if(isset($user['userID']) && $user['userID'] == $userID){
					$inSeat = true;
				}
			}
		}
		
		$inLook = in_array($userID,$gameInfo['lookInfo']);		
		if(!$inSeat && !$inLook ){
			$this->socketWrite(array('code'=>0,'data'=>'user not in this room'));return;
		}
		return $gameInfo;
	}
		/**
		 * 将扑克按照值进行排序 --降序
		 * @param unknown_type $pkArr
		 */
		protected function sortPk($pkArr){
			if(is_array($pkArr)){
				for($i=0;$i<count($pkArr)-1;$i++){
					for($j=$i+1;$j<count($pkArr);$j++){
						if($pkArr[$i][1]<$pkArr[$j][1]){//换位
							$c = $pkArr[$i];
							$pkArr[$j] = $pkArr[$i];
							$pkArr[$i] = $c;
						}
					}
				}
			}
			return $pkArr;
		}
		/**
		 * 获取用户手中牌的牌面值 4 3 2
		 * @param unknown_type $userPk
		 * @param unknown_type $publicPk
		 */
        protected function getPkScore($userPk,$publicPk){
    		$pkArr = array_merge($userPk,$publicPk);
    		$pkArr = $this->sortPk($pkArr);
    		$sScore = 0;
    		for($i=0;$i<count($pkArr);$i++){
    			$sScore += $pkArr[$i][1] * pow(10,count($pkArr)-$i-1);
    		}
    		
    		$styleScore = $this->getPkStyle($pkArr);
    		$sScore += ($styleScore*pow(10,count($pkArr)));
    		return $sScore;
        }
        /**
         * 分析牌型 并返回对应的牌型值
         * @param unknown_type $pkArr
         */
        protected function getPkStyle($pkArr){
        	$pkArr = $this->sortPk($pkArr);
        	//同花顺
        	if(($pkArr[0][0]==$pkArr[1][0]&&$pkArr[0][0]==$pkArr[2][0])&&($pkArr[0][1]==$pkArr[1][1]+1&&$pkArr[1][1]==$pkArr[2][1]+1))
        		return 50;
        	//三条
        	if($pkArr[0][1]==$pkArr[1][1]&&$pkArr[0][1]==$pkArr[2][1])
        		return 40;
        	//同花
        	if($pkArr[0][0]==$pkArr[1][0]&&$pkArr[0][0]==$pkArr[2][0])
        		return 30;
        	//顺子
        	if($pkArr[0][1]==$pkArr[1][1]+1&&$pkArr[1][1]==$pkArr[2][1]+1)
        		return 20;
        	//对子
        	if($pkArr[0][1]==$pkArr[1][1] || $pkArr[0][1]==$pkArr[2][1])
        		return 10+$pkArr[0][1];
        	if($pkArr[1][1]==$pkArr[2][1])
        		return 10+$pkArr[1][1];
        	return 0;
        }       
        
        
        /*准备*/
        /* public function prepare(){
        	$runData = $this->getRunData();
			if(!isset($runData['userID'],$runData['roomID'])){
				$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return false;
			}
			$gameModel = $this->loadModel("Game");
			$gameInfo = $this->gameInfo($runData['roomID'], $runData['userID']);			
			if(!$gameInfo)
				return false;
			$userInfo = $gameInfo['userInfo'];
			if(is_array($userInfo)){
				foreach($userInfo as &$user){
					if(isset($user['userID']) && $user['userID'] == $runData['userID']){
						$haveUserInfo = true;
						$user['status'] = 2;
					}
				}
			}			
			$gameModel->update(array('id'=>$gameInfo['id'],'userInfo'=>serialize($userInfo)));
			$newGameInfo = $gameInfo;
			$newGameInfo['userInfo'] = $userInfo;
			$this->socketWrite(array('code'=>1,'error'=>'','data'=>$newGameInfo));return false;
			return $newGameInfo;
		} */
        
        /*开始*/
        public function startGame(){ //包括发牌
        	$runData = $this->getRunData();
        	if(!isset($runData['userID'],$runData['roomID'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
        	}
        	$gameModel = $this->loadModel("Game");
        	$gameInfo = $this->gameInfo($runData['roomID'], $runData['userID']);
        	if(!$gameInfo)
        		return false;

        	if($gameInfo['status'] != 0){
        		$this->socketWrite(array('code'=>0,'data'=>'游戏已经开始！不能重新发牌'));return;
        	}
        	if(count($gameInfo['userInfo'])<2){
        		$this->socketWrite(array('code'=>0,'data'=>'至少2个人才可以开始游戏'));return;
        	}        	
        	//发牌
        	$pkList   = $this->pkList;
        	$publicPk = array(); //公共牌
        	$publicPkNum = 1;
        	for($i=0;$i<$publicPkNum;$i++){
        		$k = array_rand($pkList);
        		$publicPk[] = $pkList[$k];
        		unset($pkList[$k]);
        	}        	
        	$userInfo = $gameInfo['userInfo'];
                
            $roomModel = $this->loadModel('Room');
            $roomInfo  = $roomModel->getRowByWhere(array('id'=>$runData['roomID']));
        	//print_r($userInfo);exit;
                $i = 0;
        	foreach ($userInfo as &$user){
        		$user['status'] = 9;
        		$userpk = array();
        		$key1 = array_rand($pkList);        		
        		$userpk[] = $pkList[$key1];        		
        		unset($pkList[$key1]);
        		$key2 = array_rand($pkList);
        		$userpk[] = $pkList[$key2];
        		unset($pkList[$key2]);                        
        		$user['pk'] = $userpk;
        		$user['pkVal'] = $this->getPkScore($userpk, $publicPk);
                        $i++;
        	}
        	
        	$gameInfo['publicPk'] = $publicPk;
        	//发牌结束        	
        	//小盲注
        	$xmz = $this->nextAct($userInfo, $gameInfo['banker'],$gameInfo['banker']);        	        	
        	$xmzMoney = floor($roomInfo['lBlind']/2);
        	$userInfo[$xmz]['status'] = 2; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
        	$userInfo[$xmz]['jetton'] -= $xmzMoney; //更新用户的钱币
        	$userInfo[$xmz]['costJetton'] += $xmzMoney; //更新用户本轮消耗的钱币
        	$userInfo[$xmz]['allcostJetton'] += $xmzMoney; //更新用户本局消耗的钱币
        	$gameInfo['money'] += $xmzMoney; //更新桌面现有钱数
        	//大盲注
        	$dmz = $this->nextAct($userInfo, $xmz,$xmz);        	
        	$dmzMoney = $roomInfo['lBlind'];
        	$userInfo[$dmz]['status'] = 2; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
        	$userInfo[$dmz]['jetton'] -= $dmzMoney; //更新用户的钱币
        	$userInfo[$dmz]['costJetton'] += $dmzMoney; //更新用户本轮消耗的钱币
        	$userInfo[$dmz]['allcostJetton'] += $dmzMoney; //更新用户本局消耗的钱币
        	$gameInfo['money'] += $dmzMoney; //更新桌面现有钱数        	
        	$gameInfo['userInfo'] = $userInfo;
        	//$gameInfo['banker']   = $this->nextSeat($userInfo, $gameInfo['banker']);
        	
        	$gameInfo['next']     =  $this->nextAct($userInfo, $dmz,$dmz);
        	$gameInfo['status']   = 1;
        	$gameModel->saveGame($gameInfo);
        	$this->reGameInfo($gameInfo,$runData['roomID']);return ;
        	
		}
		protected function addJetton($gameInfo,$roomID,$userID,$jetton,$status){
			if(!in_array($status, array(0,1,2,3,4))){
				$this->socketWrite(array('code'=>0,'data'=>'系统错误'));return;
			}
			$gameModel = $this->loadModel("Game");
			
		}
		/**
		 * 判断一轮心动是否结束
		 * @param unknown_type $gameInfo
		 */
		protected function isActOver($gameInfo){
			$userInfo = $gameInfo['userInfo'];
			$max = 0;
			$isOver = true;
			if($userInfo){
				foreach($userInfo as $user){
					if($user['status'] !=0){
                                            if($max < $user['costJetton'] ){
						$max = $user['costJetton'];
                                            }
					}
				}
				foreach($userInfo as $user){
					if($user['status'] !=0 && $user['costJetton'] < $max && $user['jetton'] > 0){
						$isOver = false;
						break;
					}
				}
			}
			if($isOver){ //玩家出牌结束之后把钱放到奖池中 清空用户的costJetton
				foreach($userInfo as $user){
					$user['costJetton'] =0;					
				}
			}
			return $isOver;
		}
		
		protected function Act($gameInfo){
			//如果只剩下一个玩家 直接状态变为5
			$liveNum = 0;
			if($gameInfo['userInfo']){
				foreach ($gameInfo['userInfo'] as $user){
					if($user['status'] != 0){
						$liveNum++;
					}
				}
			}
			if($liveNum <= 1){
				$gameInfo['status'] = 5;
				return;
			}
			
			switch ($gameInfo['status']){
				case 1: //发牌状态 第一个玩家行动改变房间状态为2
					break;
				case 2: //第一轮游戏 判断是否行动结束状态+1 next变为-1
					if($this->isActOver($gameInfo)){
						$gameInfo['status'] = 3;	
						$gameInfo['next']   = -1;
					}	
					break;
				case 3://发完公共牌 第一个玩家行动
					
					break;
				case 4://第二轮心动 判断是否行动结束
					if($this->isActOver($gameInfo)){
						$gameInfo['status'] = 5;
						$gameInfo['next']   = -2;
					}
					break;
				
			}
            return $gameInfo;
		}
        /*跟注
         * //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
         * */
        public function cingl(){
            $runData = $this->runData;
            if(!isset($runData['userID'],$runData['roomID'],$runData['jetton'])){
            	$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
            }
            $userID = $this->runData['userID'];
            $roomID = $this->runData['roomID'];            
            $jetton = $this->runData['jetton'];            
            $gameModel = $this->loadModel("Game");
            //获取房间信息
            $gameInfo  = $this->gameInfo($roomID,$userID);//$gameModel->getGameInfo(array("roomID"=>$roomID));
            if(!$gameInfo)
            	return false;
            if( !in_array($gameInfo['status'],array(1,2,3,4))){
                $this->socketWrite(array('code'=>0,'error'=>'','data'=>' the game is playing '));return;
            }
            //判断是是行动者
            $curAct = $gameInfo['next'];
            if(!isset($gameInfo['userInfo'][$curAct]['userID']) || $gameInfo['userInfo'][$curAct]['userID'] != $userID){
            	$this->socketWrite(array('code'=>0,'error'=>'','data'=>'本轮不该你行动'));return;
            }
         	foreach ($gameInfo['userInfo'] as $skey=>$user){
				if($user['userID'] == $userID){
					$seat = $skey;break;
				}
			}
			/*修改房间基础信息*/
			$gameInfo['next'] =  $this->nextAct($gameInfo['userInfo'], $seat, $seat); //更新下一个出牌的玩家
			$gameInfo['userInfo'][$seat]['status'] = 1; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
			$gameInfo['userInfo'][$seat]['jetton'] -= $jetton; //更新用户的钱币
			$gameInfo['userInfo'][$seat]['costJetton'] += $jetton; //更新用户的消耗钱币
			$gameInfo['userInfo'][$seat]['allcostJetton'] += $jetton; //更新用户的消耗钱币
			$gameInfo['money'] += $jetton; //更新桌面现有钱数
			/*存入数据库*/
                        //print_R($gameInfo);exit;
                        $gameInfo = $this->Act($gameInfo);
			$gameModel->saveGame($gameInfo);
			
			$this->reGameInfo($gameInfo, $roomID);
			return ;
            /*返回房间信息*/
            //$this->socketWrite(array('code'=>1,'error'=>'','data'=>$gameInfo));return;    
	}
        
        /*加注*/
        public function raise(){
            $runData = $this->runData;
        	if(!isset($runData['userID'],$runData['roomID'],$runData['jetton'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
        	}
            $userID = $this->runData['userID'];
            $roomID = $this->runData['roomID'];            
            $jetton = $this->runData['jetton'];            
            $gameModel = $this->loadModel("Game");
            //获取房间信息
            $gameInfo  = $this->gameInfo($roomID,$userID);//$gameModel->getGameInfo(array("roomID"=>$roomID));
            if(!$gameInfo)
            	return false;
            if( !in_array($gameInfo['status'],array(1,2,3,4))){
                $this->socketWrite(array('code'=>0,'error'=>'','data'=>' the game is playing '));return;
            }
            //判断是是行动者
            $curAct = $gameInfo['next'];
            if(!isset($gameInfo['userInfo'][$curAct]['userID']) || $gameInfo['userInfo'][$curAct]['userID'] != $userID){
            	$this->socketWrite(array('code'=>0,'error'=>'','data'=>'本轮不该你行动'));return;
            }
         	foreach ($gameInfo['userInfo'] as $skey=>$user){
				if($user['userID'] == $userID){
					$seat = $skey;break;
				}
			}
			/*修改房间基础信息*/
			$gameInfo['next'] = $this->nextAct($gameInfo['userInfo'], $seat,$seat); //更新下一个出牌的玩家
			$gameInfo['userInfo'][$seat]['status'] = 2; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
			$gameInfo['userInfo'][$seat]['jetton'] -= $jetton; //更新用户的钱币
			$gameInfo['userInfo'][$seat]['costJetton'] += $jetton; //更新用户的消耗钱币
			$gameInfo['userInfo'][$seat]['allcostJetton'] += $jetton; //更新用户的消耗钱币
			$gameInfo['money'] += $jetton; //更新桌面现有钱数
			/*存入数据库*/
			$gameInfo = $this->Act($gameInfo);
			$gameModel->saveGame($gameInfo);
			$this->reGameInfo($gameInfo, $roomID);
			return ;
        }
        /*
         * 获取正常状态下牌值最大得分
         */
        protected function getMScoreAndLMoney($userInfo){
        	//$maxUser = current($userInfo);
        	//$maxUser['pkVal'] = isset($maxUser['pkVal'])?$maxUser['pkVal']:0;
        	$maxUser = null;
        	foreach($userInfo as $user){ 
        		if($user['status'] == 0)
        			continue;
        		if(!$maxUser){
        			$maxUser = $user;
        			continue;
        		}       		
        		if(isset($user['pkVal']) && $user['pkVal']>$maxUser['pkVal']){
        			$maxUser = $user;
        		}
        		if(isset($user['pkVal']) && $user['pkVal'] == $maxUser['pkVal'] && $user['allcostJetton'] < $maxUser['allcostJetton']){
        			$maxUser = $user;
        		}
        	}
        	return $maxUser;
        }
        /**
         * 结算用户信息
         */
		protected function jiesuan($userInfo){
			
			$maxUser = $this->getMScoreAndLMoney($userInfo);								
			if($maxUser){
				//结算当前用户筹码
				$winUser = 0;
				$winMoney = 0; 
				foreach ($userInfo as &$user){					
					if($user['pkVal'] == $maxUser['pkVal'] && $user['status'] != 0){
						$winUser++;
					}
					if($user['allcostJetton'] > $maxUser['allcostJetton']){
						$winMoney += $maxUser['allcostJetton'];
						$user['allcostJetton'] -= $maxUser['allcostJetton']; //
					}else{
						$winMoney += $user['allcostJetton'];
						$user['allcostJetton'] = 0; //
					}
				}						
				//计算每人分多少钱
				$agvMoney = $winUser>0?floor($winMoney/$winUser):0;				
				foreach($userInfo as &$user){ //给赢钱用户加钱
					if($user['pkVal'] == $maxUser['pkVal'] && $user['status'] != 0){
						$user['winJetton'] += $agvMoney;						
					}
					if($user['allcostJetton'] <= 0)
						$user['status'] = 0; //将用户状态变为弃牌状态
				}				
				$userInfo = $this->jiesuan($userInfo);
			}
			return $userInfo;
		}
        /*全下*/
        public function allIn(){
            $runData = $this->runData;
        	if(!isset($runData['userID'],$runData['roomID'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
        	}
            $userID = $this->runData['userID'];
            $roomID = $this->runData['roomID'];            
            $gameModel = $this->loadModel("Game");
            //获取房间信息
            $gameInfo  = $this->gameInfo($roomID,$userID);//$gameModel->getGameInfo(array("roomID"=>$roomID));
            if(!$gameInfo)
            	return false;
            
            if( !in_array($gameInfo['status'],array(1,2,3,4))){
                $this->socketWrite(array('code'=>0,'error'=>'','data'=>' the game is playing '));return;
            }
            //判断是是行动者
            $curAct = $gameInfo['next'];
            if(!isset($gameInfo['userInfo'][$curAct]['userID']) || $gameInfo['userInfo'][$curAct]['userID'] != $userID){
            	$this->socketWrite(array('code'=>0,'error'=>'','data'=>'本轮不该你行动'));return;
            }   
         	foreach ($gameInfo['userInfo'] as $skey=>$user){
				if($user['userID'] == $userID){
					$seat = $skey;break;
				}
			}
			$jetton = $gameInfo['userInfo'][$seat]['jetton'];
			/*修改房间基础信息*/
			$gameInfo['next'] =  $this->nextAct($gameInfo['userInfo'], $seat,$seat); //更新下一个出牌的玩家
			$gameInfo['userInfo'][$seat]['status'] = 3; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
			$gameInfo['userInfo'][$seat]['jetton'] -= $jetton; //更新用户的钱币
			$gameInfo['userInfo'][$seat]['costJetton'] += $jetton; //更新用户的消耗钱币
			$gameInfo['userInfo'][$seat]['allcostJetton'] += $jetton; //更新用户的消耗钱币
			$gameInfo['money'] += $jetton; //更新桌面现有钱数
			/*存入数据库*/
			$gameInfo = $this->Act($gameInfo);
			$gameModel->saveGame($gameInfo);
			$this->reGameInfo($gameInfo, $roomID);
			return ;
        }
        
        /*弃牌*/
        public function discard(){
            $runData = $this->runData;
        	if(!isset($runData['userID'],$runData['roomID'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
        	}
            $userID = $this->runData['userID'];
            $roomID = $this->runData['roomID'];            
            $gameModel = $this->loadModel("Game");
            //获取房间信息
            $gameInfo  = $this->gameInfo($roomID,$userID);//$gameModel->getGameInfo(array("roomID"=>$roomID));
            if(!$gameInfo)
            	return false;
            if( !in_array($gameInfo['status'],array(1,2,3,4))){
                $this->socketWrite(array('code'=>0,'error'=>'','data'=>' the game is playing '));return;
            }
            //判断是是行动者
            $curAct = $gameInfo['next'];
            if(!isset($gameInfo['userInfo'][$curAct]['userID']) || $gameInfo['userInfo'][$curAct]['userID'] != $userID){
            	$this->socketWrite(array('code'=>0,'error'=>'','data'=>'本轮不该你行动'));return;
            }
         	foreach ($gameInfo['userInfo'] as $skey=>$user){
				if($user['userID'] == $userID){
					$seat = $skey;break;
				}
			}
			$jetton = $gameInfo['userInfo'][$seat]['jetton'];
			/*修改房间基础信息*/
			$gameInfo['next'] = $this->nextAct($gameInfo['userInfo'], $seat,$seat); //更新下一个出牌的玩家
			$gameInfo['userInfo'][$seat]['status'] = 0; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
			
                        /*存入数据库*/
			$gameInfo = $this->Act($gameInfo);
			$gameModel->saveGame($gameInfo);
			$this->reGameInfo($gameInfo, $roomID);
			return ; 
        }     
        
        /*看牌*/
        public function pass(){
            $runData = $this->runData;
        	if(!isset($runData['userID'],$runData['roomID'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
        	}
            $userID = $this->runData['userID'];
            $roomID = $this->runData['roomID'];            
            $gameModel = $this->loadModel("Game");
            //获取房间信息
            $gameInfo  = $this->gameInfo($roomID,$userID);//$gameModel->getGameInfo(array("roomID"=>$roomID));
            if(!$gameInfo)
            	return false;
            if( !in_array($gameInfo['status'],array(1,2,3,4))){
                $this->socketWrite(array('code'=>0,'error'=>'','data'=>' the game is playing '));return;
            }
            //判断是是行动者
            $curAct = $gameInfo['next'];                        
            if(!isset($gameInfo['userInfo'][$curAct]['userID']) || $gameInfo['userInfo'][$curAct]['userID'] != $userID){
            	$this->socketWrite(array('code'=>0,'error'=>'','data'=>'本轮不该你行动'));return;
            }            
         	foreach ($gameInfo['userInfo'] as $skey=>$user){
				if($user['userID'] == $userID){
					$seat = $skey;break;
				}
			}
			/*修改房间基础信息*/
			$gameInfo['next'] =  $this->nextAct($gameInfo['userInfo'], $seat,$seat); //更新下一个出牌的玩家
			$gameInfo['userInfo'][$seat]['status'] = 4; //0弃牌 1 跟注 2 加注 3 全下 4看牌  //更新用户的当前状态
			/*存入数据库*/
			$gameInfo = $this->Act($gameInfo);
			$gameModel->saveGame($gameInfo);
			$this->reGameInfo($gameInfo, $roomID);
			return ;
        
        }
        
        /*结束*/
        public function endGame(){
        	$runData = $this->getRunData();
        	if(!isset($runData['userID'],$runData['roomID'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return false;
        	}
        	$gameModel = $this->loadModel("Game");
        	$gameInfo = $this->gameInfo($runData['roomID'], $runData['userID']);
        	if($gameInfo['status'] != 5){
        		$this->socketWrite(array('code'=>0,'data'=>'游戏没有到结束状态'));return false;
        	}
        	$gameInfo['userInfo'] = $this->jiesuan($gameInfo['userInfo']);
                $gameInfo['status']   = 6;
            //$newUser = $this->cleanNoMoneyHuman($gameInfo['userInfo'] , $gameInfo['lookInfo'] );
        	//$gameInfo['userInfo'] = $newUser['user'];
            //$gameInfo['lookInfo'] = $newUser['looking'];            
        	$gameModel->saveGame($gameInfo);
        	$this->reGameInfo($gameInfo,$runData['roomID']);        	
        	//$this->socketWrite(array('code'=>1,'error'=>'','data'=>$gameInfo));return;
		}
        /*清除钱币为0的用户,变更状态为观战*/
        private function cleanNoMoneyHuman( $userInfo ,$looking ){
            foreach ( $userInfo as $k=>$v ){
                if( 0 == $v['jetton'] && 0 == $v['costJetton'] && 0 == $v['winJetton']  ){
                    $looking[$v['id']] = $v['id'];
                }else{
                    $uinfo[$k] = $v;
                }
            }
            $arr['user'] = $uinfo;
            $arr['looking'] = $looking;
            return $arr;
        }
        /*修改状态*/
        public function changeStatus(){
            $runData = $this->runData;
        	if(!isset($runData['userID'],$runData['roomID'])){
        		$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
        	}
            $userID = $this->runData['userID'];
            $roomID = $this->runData['roomID'];            
            $gameModel = $this->loadModel("Game");
            //获取房间信息
            $gameInfo  = $this->gameInfo($roomID,$userID);//$gameModel->getGameInfo(array("roomID"=>$roomID));
            if(!$gameInfo)
            	return false;
            $userAct = true;
            $lookAct = true;
            if( !in_array($userID, $gameInfo['actUser'])){
                $gameInfo['actUser'][$userID] = $userID;
            }
            if(1 < count($gameInfo['userInfo'])){
                foreach($gameInfo['userInfo'] as $v ){
                    if(!isset( $gameInfo['actUser'][$v['userID']])){
                        $userAct = false;
                    }
                }
            }
            if(1 < count($gameInfo['lookInfo'])){
                foreach($gameInfo['lookInfo'] as $v ){
                    if(!isset( $gameInfo['actUser'][$v])){
                        $userAct = false;
                    }
                }
            }
            if( $userAct && $lookAct ){
                $gameInfo['actUser'] = array();
                if( 6 == $gameInfo['status'] ){
                    $gameInfo['status'] = 0;
                    $gameInfo['publicPk'] = array();
                    $gameInfo['money'] = 0;
                    $gameInfo['userInfo'] = $this->userStatuChange( $gameInfo['userInfo'] );
                    $next = $this->nextSeat($gameInfo['userInfo'], $gameInfo['banker']);
                    $gameInfo['banker'] = $next ;
                    $gameInfo['next']= -1 ;
                }else{
                    $gameInfo['status'] += 1;
                }
                
            }
            $gameModel->saveGame($gameInfo);
            $this->socketWrite(array('code'=>1,'data'=>'success'));return;
        }
        public function userStatuChange($userinfo){
            if(!empty($userinfo)){
                foreach ($userinfo as $k=>$v ){
                    $v['pk'] = array(); 
                    $v['status'] = 0; 
                    $v['jetton'] = $v['jetton']+$v['winJetton'];
                    $v['winJetton'] = 0;
                    $v['costJetton'] = 0;
                    $v['allcostJetton'] = 0;
                    $v['pkVal'] = 0;
                    $info[$k] = $v;
                }
                return $info;
            }else{
                return array();
            }
        }
        /**
         * 每秒中用户轮训该房间内信息
         */
		public function getGameInfo(){
			$runData = $this->getRunData();
			if(!isset($runData['userID'],$runData['roomID'])){
				$this->socketWrite(array('code'=>0,'data'=>'parameter error'));return;
			}
                        
			$gameInfo = $this->gameInfo($runData['roomID'], $runData['userID']);	
                        
			if(!$gameInfo)
				return false;				
			$this->reGameInfo($gameInfo,$runData['roomID']);
			return ;
		}
        
}